<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogEditVisible" :before-close="handleClose">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="130px" style="margin-left:50px;margin-right:50px">

      <el-form-item label="外网主机地址" prop="media_ip">
        <el-input v-model="temp.media_ip" placeholder="生成对外的播放地址" />
      </el-form-item>
      <el-form-item label="内网主机地址" prop="intranet">
        <el-input v-model="temp.intranet" placeholder="媒体服务器与本国标服务通讯的地址" />
      </el-form-item>
      <el-form-item label="secret" prop="zl_secret">
        <el-input v-model="temp.zl_secret" placeholder="对应ZlMediaKit[api]下的secret" />
      </el-form-item>
      <el-form-item label="http端口号" prop="http_port">
        <el-input v-model="temp.http_port" placeholder="对应ZlMediaKit[http]下的port" />
      </el-form-item>
      <el-form-item label="服务器配置" prop="config">
        <el-button @click="loadServerConfig()">点击查询所填服务器配置</el-button>
      </el-form-item>
      <el-form-item label="mediaServerId" prop="server_id_zlk">
        <el-input v-model="temp.server_id_zlk" placeholder="对应ZlMediaKit[general]下的mediaServerId" />
      </el-form-item>
      <el-form-item label="http ssl port" prop="https_port">
        <el-input v-model="temp.https_port" placeholder="对应ZlMediaKit[http]下的sslport" />
      </el-form-item>
      <el-form-item label="rtmp port" prop="rtmp_port">
        <el-input v-model="temp.rtmp_port" placeholder="对应ZlMediaKit[rtmp]下的port" />
      </el-form-item>
      <el-form-item label="rtp起始端口" prop="rtp_min_port">
        <el-input v-model="temp.rtp_min_port" placeholder="可设置为50000,防火墙需要开放端口段" />
      </el-form-item>
      <el-form-item label="rtp截止端口" prop="rtp_max_port">
        <el-input v-model="temp.rtp_max_port" placeholder="可设置为50500,防火墙需要开放端口段" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogEditVisible = false">
        取消
      </el-button>
      <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
        提交
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { Message } from 'element-ui'
import { getServerInfo } from '@/api/zlMediaKit'
import { insert, update } from '@/api/mediaServer'

export default {
  name: 'MediaServerEdit',
  props: {
    dialogStatus: {
      type: String,
      default: 'create'
    },
    show: {
      type: Boolean,
      default: false
    },
    editItem: {
      type: Object,
      default: () => {
        return {
          media_ip: '',
          intranet: '',
          zl_secret: '',
          http_port: '',
          server_id_zlk: '',
          https_port: '',
          rtmp_port: '',
          rtp_min_port: '',
          rtp_max_port: ''
        }
      }
    }
  },
  data() {
    return {
      textMap: {
        update: '修改媒体服务器',
        create: '新增媒体服务器'
      },
      rules: {
        server_id: [{ required: true, message: 'ServerId不能为空', trigger: 'change' }],
        media_ip: [{ required: true, message: '外网主机地址不能为空', trigger: 'change' }],
        intranet: [{ required: true, message: '内网主机地址不能为空', trigger: 'change' }],
        http_port: [{ required: true, message: 'http 服务端口号不能为空', trigger: 'change' }],
        rtmp_port: [{ required: true, message: 'rtmp 服务端口号不能为空', trigger: 'change' }],
        rtp_min_port: [{ required: true, message: 'rtp 起始端口号不能为空', trigger: 'change' }],
        rtp_max_port: [{ required: true, message: 'rtp 截止端口号不能为空', trigger: 'change' }]
      },
      temp: {},
      dialogEditVisible: false
    }
  },
  watch: {
    editItem(val) {
      this.temp = val
    },
    show(val) {
      this.dialogEditVisible = val
    }
  },
  methods: {
    loadServerConfig() {
      if (!this.temp.intranet) {
        Message({
          message: '请先填写主机地址',
          type: 'error',
          duration: 5 * 1000
        })
      } else {
        getServerInfo({
          'host': this.temp.intranet,
          'port': this.temp.http_port,
          'zl_secret': this.temp.zl_secret
        }).then((ret) => {
          if (ret.code === 20000) {
            const config = ret.data[0]
            this.temp.https_port = config['http.sslport']
            this.temp.rtmp_port = config['rtmp.port']
            this.temp.server_id_zlk = config['general.mediaServerId']
          }
        })
      }
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.listLoading = true
          insert(this.temp).then((ret) => {
            if (ret.code === 20000) {
              this.dialogEditVisible = false
              this.$notify({
                title: 'Success',
                message: 'Created Successfully',
                type: 'success',
                duration: 2000
              })
              this.$emit('update:show', false)
            }
          }).finally(() => {
            this.listLoading = false
          })
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
          this.listLoading = true
          update(this.temp).then((ret) => {
            if (ret.code === 20000) {
              this.dialogEditVisible = false
              this.$notify({
                title: 'Success',
                message: '更新操作成功',
                type: 'success',
                duration: 2000
              })
              this.$emit('update:show', false)
            }
          }).finally(() => {
            this.listLoading = false
          })
        }
      })
    },
    handleClose(done) {
      this.$emit('update:show', false)
      done()
    }
  }
}
</script>

<style scoped>

</style>
